﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;


public partial class UserControl_ControlChietKhau : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
                if (ViewState["flag"] == null)
                {
                    ViewState["flag"] = false;
                }
           
            //nằm trong thread
            ThreadStart thr1 = new ThreadStart(LoadGrid);
            Thread tr1 = new Thread(thr1);

            ThreadStart thr2 = new ThreadStart(BindMember);
            Thread tr2 = new Thread(thr2);

            tr1.Start();
            tr2.Start();
            //nằm trong thread

        }
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        txtMaSo.Text = "New";
        txtName.Text = "";
        txtDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
        txtCK.Text = "";
        ViewState["flag"] = true;
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        int i=0;
        try
        {
            if ((bool)ViewState["flag"])
            {
                //insert
                i = dt.ExecuteSQl_int(str, "k_insertDiscount", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@Des", txtName.Text),
                    new SqlParameter("@Recdate", txtDate.Text),
                    new SqlParameter("@MemberID", ddlDoiTuong.SelectedValue.ToString()),
                    new SqlParameter("@Rate", decimal.Parse(txtCK.Text)));
                if (i == 1)
                {
                    uscMsgBox1.AddMessage("Thêm mới chiết khấu thành công", uscMsgBox.enmMessageType.Success);
                }
                else
                {
                    uscMsgBox1.AddMessage("Hệ thống đang bảo trì", uscMsgBox.enmMessageType.Error);
                }
                ViewState["flag"] = false;
                txtMaSo.Text = "";
                txtName.Text = "";
                txtCK.Text = "";
            }
            else
            {
                //update
                i = dt.ExecuteSQl_int(str, "k_updateDiscount", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@Des", txtName.Text),
                    new SqlParameter("@Recdate", txtDate.Text),
                    new SqlParameter("@MemberID", ddlDoiTuong.SelectedValue.ToString()),
                    new SqlParameter("@Rate", decimal.Parse(txtCK.Text)),
                    new SqlParameter("@DiscountID",int.Parse(txtMaSo.Text)));
                if (i == 1)
                {
                    uscMsgBox1.AddMessage("Cập nhật chiết khấu thành công", uscMsgBox.enmMessageType.Success);
                }
                else
                {
                    uscMsgBox1.AddMessage("Đề nghị bạn cung cấp mã chiết khấu để cập nhật", uscMsgBox.enmMessageType.Error);
                }

            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }       
    }
    protected void imgBtnDelete_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void imgBtnFind_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    protected void grvChietKhau_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (e.CommandName == "chon")
            {
                GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                Label lblMaSo = (Label)grv.FindControl("lblMaSo");

                DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getDetail", CommandType.StoredProcedure,
                    new SqlParameter("@DiscountID", lblMaSo.Text));
                
                txtMaSo.Text = db.Rows[0]["DiscountID"].ToString();
                txtName.Text = db.Rows[0]["Des"].ToString();
                txtDate.Text=db.Rows[0]["Recdate"].ToString();
                
                ddlDoiTuong.SelectedValue = db.Rows[0]["MemberID"].ToString();
                txtCK.Text=db.Rows[0]["Rate"].ToString();

                LoadGrid();

                ViewState["flag"] = false;
                TabContainer1.ActiveTabIndex = 0;
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }    
    }
    void LoadGrid()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getDiscount", CommandType.StoredProcedure);
            grvChietKhau.DataSource = db;
            grvChietKhau.DataBind();
            if (db.Rows.Count == 0)
            {
                uscMsgBox1.AddMessage("Không tìm thấy kết quả", uscMsgBox.enmMessageType.Attention);
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    void BindMember()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getMemberDiscount", CommandType.StoredProcedure);
        dt.BindDropDownList(db, ddlDoiTuong, "MemberID", "Des");
    }
}